#include <stdio.h>
#include <windows.h>
#include <assert.h>



const unsigned char ptz_cmd[][7]={
{0xFF,0x01,0x00,0x4D,0x00,0x00,0x4E},//发送命令1=(垂直0度):FF 01 00 4D 00 00 4E
{0xFF,0x01,0x00,0x4D,0x80,0xE8,0xB6},//发送命令2=(垂直30度):FF 01 00 4D 80 E8 B6
{0xFF,0x01,0x00,0x4D,0x75,0x30,0xF3},//发送命令3=(垂直60度):FF 01 00 4D 75 30 F3
{0xFF,0x01,0x00,0x4D,0x6D,0x60,0x1B},//发送命令4=(垂直80度):FF 01 00 4D 6D 60 1B
{0xFF,0x01,0x00,0x4D,0x69,0xDC,0x93},//发送命令5=(垂直90度):FF 01 00 4D 69 DC 93
{0xFF,0x01,0x00,0x4D,0x6D,0x60,0x1B},//发送命令6=(垂直80度):FF 01 00 4D 6D 60 1B
{0xFF,0x01,0x00,0x4D,0x75,0x30,0xF3},//发送命令7=(垂直60度):FF 01 00 4D 75 30 F3
{0xFF,0x01,0x00,0x4D,0x80,0xE8,0xB6},//发送命令8=(垂直30度):FF 01 00 4D 80 E8 B6
{0xFF,0x01,0x00,0x4D,0x00,0x00,0x4E},//发送命令9=(垂直0度):FF 01 00 4D 00 00 4E
{0xFF,0x01,0x00,0x4D,0x03,0xE8,0x39},//发送命令10=(垂直-10度):FF 01 00 4D 03 E8 39
{0xFF,0x01,0x00,0x4D,0x0F,0xA0,0xFD},//发送命令11=(垂直-40度):FF 01 00 4D 0F A0 FD
{0xFF,0x01,0x00,0x4D,0x13,0x88,0xE9},//发送命令12=(垂直-50度):FF 01 00 4D 13 88 E9
{0xFF,0x01,0x00,0x4D,0x17,0x70,0xD5},//发送命令13=(垂直-60度):FF 01 00 4D 17 70 D5
{0xFF,0x01,0x00,0x4D,0x1B,0x58,0xC1},//发送命令14=(垂直-70度):FF 01 00 4D 1B 58 C1
{0xFF,0x01,0x00,0x4D,0x1F,0x40,0xAD},//发送命令15=(垂直-80度):FF 01 00 4D 1F 40 AD
{0xFF,0x01,0x00,0x4D,0x22,0xC4,0x34},//发送命令16=(垂直-89度):FF 01 00 4D 22 C4 34
{0xFF,0x01,0x00,0x4D,0x1F,0x40,0xAD},//发送命令17=(垂直-80度):FF 01 00 4D 1F 40 AD
{0xFF,0x01,0x00,0x4D,0x1B,0x58,0xC1},//发送命令18=(垂直-70度):FF 01 00 4D 1B 58 C1
{0xFF,0x01,0x00,0x4D,0x17,0x70,0xD5},//发送命令19=(垂直-60度):FF 01 00 4D 17 70 D5
{0xFF,0x01,0x00,0x4D,0x13,0x88,0xE9},//发送命令20=(垂直-50度):FF 01 00 4D 13 88 E9
{0xFF,0x01,0x00,0x4D,0x0F,0xA0,0xFD},//发送命令21=(垂直-40度):FF 01 00 4D 0F A0 FD
{0xFF,0x01,0x00,0x4D,0x0B,0xB8,0x11},//发送命令22=(垂直-30度):FF 01 00 4D 0B B8 11
{0xFF,0x01,0x00,0x4D,0x07,0xD0,0x25},//发送命令23=(垂直-20度):FF 01 00 4D 07 D0 25
{0xFF,0x01,0x00,0x4D,0x03,0xE8,0x39},//发送命令24=(垂直-10度):FF 01 00 4D 03 E8 39
{0xFF,0x01,0x00,0x4D,0x00,0x00,0x4E},//发送命令24+=(垂直0度):FF 01 00 4D 00 00 4E
{0xFF,0x01,0x00,0x4B,0x00,0x00,0x4C},//发送命令25=(水平0度):FF 01 00 4B 00 00 4C
{0xFF,0x01,0x00,0x4B,0x0B,0xB8,0x0F},//发送命令26=(水平30度):FF 01 00 4B 0B B8 0F
{0xFF,0x01,0x00,0x4B,0x17,0x70,0xD3},//发送命令27=(水平60度):FF 01 00 4B 17 70 D3
{0xFF,0x01,0x00,0x4B,0x23,0x28,0x97},//发送命令28=(水平90度):FF 01 00 4B 23 28 97
{0xFF,0x01,0x00,0x4B,0x2E,0xE0,0x5A},//发送命令29=(水平120度):FF 01 00 4B 2E E0 5A
{0xFF,0x01,0x00,0x4B,0x3A,0x98,0x1E},//发送命令30=(水平150度):FF 01 00 4B 3A 98 1E
{0xFF,0x01,0x00,0x4B,0x46,0x50,0xE2},//发送命令31=(水平180度):FF 01 00 4B 46 50 E2
{0xFF,0x01,0x00,0x4B,0x52,0x08,0xA6},//发送命令32=(水平210度):FF 01 00 4B 52 08 A6
{0xFF,0x01,0x00,0x4B,0x5D,0xC0,0x69},//发送命令33=(水平240度):FF 01 00 4B 5D C0 69
{0xFF,0x01,0x00,0x4B,0x69,0x78,0x2D},//发送命令34=(水平270度):FF 01 00 4B 69 78 2D
{0xFF,0x01,0x00,0x4B,0x75,0x30,0xF1},//发送命令35=(水平300度):FF 01 00 4B 75 30 F1
{0xFF,0x01,0x00,0x4B,0x80,0xE8,0xB4},//发送命令36=(水平330度):FF 01 00 4B 80 E8 B4
{0xFF,0x01,0x00,0x4B,0x00,0x00,0x4C},//发送命令37=(水平0度):FF 01 00 4B 00 00 4C
{0xFF,0x01,0x00,0x4B,0x80,0xE8,0xB4},//发送命令38=(水平330度):FF 01 00 4B 80 E8 B4
{0xFF,0x01,0x00,0x4B,0x75,0x30,0xF1},//发送命令39=(水平300度):FF 01 00 4B 75 30 F1
{0xFF,0x01,0x00,0x4B,0x69,0x78,0x2D},//发送命令40=(水平270度):FF 01 00 4B 69 78 2D
{0xFF,0x01,0x00,0x4B,0x5D,0xC0,0x69},//发送命令41=(水平240度):FF 01 00 4B 5D C0 69
{0xFF,0x01,0x00,0x4B,0x52,0x08,0xA6},//发送命令42=(水平210度):FF 01 00 4B 52 08 A6
{0xFF,0x01,0x00,0x4B,0x46,0x50,0xE2},//发送命令43=(水平180度):FF 01 00 4B 46 50 E2
{0xFF,0x01,0x00,0x4B,0x3A,0x98,0x1E},//发送命令44=(水平150度):FF 01 00 4B 3A 98 1E
{0xFF,0x01,0x00,0x4B,0x2E,0xE0,0x5A},//发送命令45=(水平120度):FF 01 00 4B 2E E0 5A
{0xFF,0x01,0x00,0x4B,0x23,0x28,0x97},//发送命令46=(水平90度):FF 01 00 4B 23 28 97
{0xFF,0x01,0x00,0x4B,0x17,0x70,0xD3},//发送命令47=(水平60度):{0xFF 01 00 4B 17 70 D3
{0xFF,0x01,0x00,0x4B,0x0B,0xB8,0x0F},//发送命令48=(水平30度):FF 01 00 4B 0B B8 0F
{0xFF,0x01,0x00,0x4B,0x00,0x00,0x4C},//发送命令49=(水平0度):FF 01 00 4B 00 00 4C
};

//Global variable
HANDLE m_hCommPort;
DWORD WINAPI ComThreadFunc();
int gap = 20;
int com_tx(HANDLE hComm,unsigned char *txbuf, unsigned char txlen);
int task = 0;

void main(int argc,char *argv[])
{	
	DCB dcb;
	char input;
	int res = 0;
	char  ComPortName[16];
	
	if(argc != 3) 
    {
        res = -1;
        goto arg_err;
	}
	for(res = 0; res<argc ; res++)
      printf("%s ",argv[res]);
    printf("\n");
      if(strcmp(argv[1],"-s") == 0)
      {
        sprintf(ComPortName,"COM%d",atoi(argv[2]));
      }
      else
      {
        res = -1;
      }
      
	 
      arg_err:
      if(res == -1)
      {
        printf("\narguments input error,%s -s 8 -t 50",argv[0]);
        printf("\nexit!\n");
        //_getch();
        return;
      }
	  

	
		/*Open a file! Here is comm!*/
		m_hCommPort= CreateFile(ComPortName,
			GENERIC_READ|GENERIC_WRITE, 
			0,
			NULL,
			OPEN_EXISTING,
			FILE_FLAG_OVERLAPPED,
			0);
		
		if(INVALID_HANDLE_VALUE==m_hCommPort)
		{
			printf("invalid handle!\n");
			return;
		}
		/*Set comm state!*/
		
		if(!SetupComm(m_hCommPort,1024,1024))	// set the inqueue&outqueue buffer
		{
			printf("Setup Comm input buffer and output buffer failed!\n");
		}
		
		if (!GetCommState(m_hCommPort,&dcb)) {
			// Handle the error.
			printf ("GetCommState failed with error %d.\n", GetLastError());
			return;
		}
		
		
		
		dcb.BaudRate = CBR_9600;     // set the baud rate
		dcb.ByteSize = 8;             // data size, xmit, and rcv
		dcb.Parity = NOPARITY;        // no parity bit
		dcb.StopBits = ONESTOPBIT;    // one stop bit		
		
		if (!SetCommState(m_hCommPort, &dcb)) {
			// Handle the error.
			printf ("SetCommState failed with error %d.\n", GetLastError());
			return;
		}
		
		
		printf("set com%s with baudrate 9600 seccussfully!\n", argv[2]);
		
		/*Set comm mask*/
		if(!SetCommMask(m_hCommPort,EV_RXCHAR|EV_RXFLAG))
		{
			printf("Set comm mask failed!");
		}

		/*Create thread!*/
		HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ComThreadFunc,NULL,0,NULL);
		task = 1;
		CloseHandle(hThread);
		
		while(1)
		{
			//main thread
			getchar();
			if(task == 0)
				return;
		}
	
}
 
DWORD WINAPI ComThreadFunc()
{
	char buff[1024]={0};
	DWORD res,factbyte;
	COMSTAT rst;
	int fError,commEvError;
	int tx_cnt = 0;
	int rx_cnt = 0;
	DWORD dwEvtMask,CommResult,readResult;
	BOOL bRet;
	BOOL fWaitingOnStat=FALSE;	// indicate if wait comm event is pending.
	OVERLAPPED osComm = {0};	// overlapped structure for comm event operations
	OVERLAPPED osReader = {0};  // overlapped structure for read operations
	OVERLAPPED osWrite = {0};
	
	printf("I am the comm thread!\n");
	
	
	osComm.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
	if (osComm.hEvent == NULL)
		printf("Create comm Event failed!\n");
	osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (osReader.hEvent == NULL)
		printf("Create reader Event failed!\n");
	
	
	
	sprintf(buff,"hello world!\n");
	
	unsigned int rec_bytes = 0;
	int index=0;
	DWORD cnt = 0;
	while(1)
	{
		printf("control cnt:%d\n",cnt++);
		WriteFile(m_hCommPort,               // Handle to the Serialport
						   ptz_cmd[index],            // Data to be written to the port 
						   7,   // No of bytes to write into the port
						   &factbyte,  // No of bytes written to the port
						   &osWrite);			   
        index++;
		index%= sizeof(ptz_cmd)/7;
		Sleep(4000);
	}
	return 0L;
}
